﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.Database.Service;

namespace T.FAS.Runtime.Page.Dao
{
    public class PageHeaderDao : DaoService<PageHeader>
    {
        public PageHeaderDao() :
           base()
        {
            DBName = "TFAS";
        }

        public override string TableName => "FAS_PageHeader";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { "StyleSheet", "Style", "CacheConfig", "BPO", "Theme", "Form", "PageStateMachines", "StateMachines", "ThemeURL", "DefaultThemeURL", "MetaDependencys", "ResponsiveGear", "LoadMode", "TemplateID", "GroupID", "GroupName", "ApplicationID", "ModuleID", "DepartmentID", "PostLimit", "MinCellCount", "BusinessType" };
        public override Dictionary<string, object> GetDataMapping(PageHeader data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("PageType");
            datas.Add("PageType", (int)(data.PageType));
            datas.Remove("ClientType");
            datas.Add("ClientType", (int)(data.ClientType));
            datas.Remove("PositionType");
            datas.Add("PositionType", (int)(data.PositionType));
            datas.Remove("AdaptiveMode");
            datas.Add("AdaptiveMode", (int)(data.AdaptiveMode));
            datas.Remove("EngineType");
            datas.Add("EngineType", (int)data.EngineType);
            return datas;
        }
        public override PageHeader ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("PageType");
            myCol.Remove("ClientType");
            myCol.Remove("PositionType");
            myCol.Remove("IsAnonymous");
            myCol.Remove("EngineType");
            var data = base.ConvertToObject(myCol, row);
            if (row["PageType"] != DBNull.Value)
                data.PageType = (PageType)Enum.Parse(typeof(PageType),
                                                Convert.ToString(row["PageType"]));
            if (row["ClientType"] != DBNull.Value)
                data.ClientType = (ClientType)Enum.Parse(typeof(ClientType),
                                                Convert.ToString(row["ClientType"]));
            if (row["PositionType"] != DBNull.Value)
                data.PositionType = (PositionType)Enum.Parse(typeof(PositionType),
                                                Convert.ToString(row["PositionType"]));
            if (row["AdaptiveMode"] != DBNull.Value)
                data.AdaptiveMode = (AdaptiveMode)Enum.Parse(typeof(AdaptiveMode),
                                                Convert.ToString(row["AdaptiveMode"]));
            if (row["EngineType"] != DBNull.Value)
                data.EngineType = (EngineType)Enum.Parse(typeof(EngineType),
                                                Convert.ToString(row["EngineType"]));
            return data;
        }
        public List<PageHeader> GetPageHeaders(string pageID)
        {
            var result = new List<PageHeader>();
            List<object> objects = base.GetObjectsByFilter(string.Format("PageID='{0}'", pageID));
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as PageHeader));
            }
            return result;
        }
    }
}
